#if defined(_MSC_VER)
#pragma once
#endif

#ifndef LM_CORE_ATOMIC_H
#define LM_CORE_ATOMIC_H

// Atomic operation for parallel
#include "lm.h"

#include <Windows.h>
#include <intrin.h>


/**
 * The following implementations are based on PBRT
 */


 // brief Atomically attempt to exchange a pointer with another value
template <typename T> inline bool atomicCompareAndExchangePtr(T **v, T *newValue, T *oldValue) {
#if defined(WIN32)
    return InterlockedCompareExchangePointer(
		reinterpret_cast<volatile PVOID *>(v), newValue, oldValue) == oldValue;
#else
  #if !defined(__clang__)
	return __sync_bool_compare_and_swap(v, oldValue, newValue);
  #else
  #if __SIZEOF_POINTER__ == 8 
	return __sync_bool_compare_and_swap(
		reinterpret_cast<long long volatile *>(v), reinterpret_cast<long long>(oldValue), 
		reinterpret_cast<long long>(newValue));
  #else
	return __sync_bool_compare_and_swap(
		reinterpret_cast<long volatile *>(v), reinterpret_cast<long>(oldValue), 
		reinterpret_cast<long>(newValue));
  #endif
  #endif
#endif
}

// brief Atomically attempt to exchange a 32-bit integer with another value
inline bool atomicCompareAndExchange(volatile int32_t *v, int32_t newValue, int32_t oldValue) {
#if defined(WIN32)
    return InterlockedCompareExchange(
		reinterpret_cast<volatile LONG *>(v), newValue, oldValue) == oldValue;
#else
	return __sync_bool_compare_and_swap(v, oldValue, newValue);
#endif
}

// brief Atomically attempt to exchange a 64-bit integer with another value
inline bool atomicCompareAndExchange(volatile int64_t *v, int64_t newValue, int64_t oldValue) {
#if defined(WIN32)
    return _InterlockedCompareExchange64(
		reinterpret_cast<volatile LONGLONG *>(v), newValue, oldValue) == oldValue;
#else
	return __sync_bool_compare_and_swap(v, oldValue, newValue);
#endif
}

 // brief Atomically add \a delta to the floating point destination \a dst
inline float atomicAdd(volatile float *dst, float delta) {
	/* Atomic FP addition from PBRT */
	union bits { float f; int32_t i; };
	bits oldVal, newVal;
	do {
        // On IA32/x64, adding a PAUSE instruction in compare/exchange loops
        // is recommended to improve performance.  (And it does!)
#if (defined(__i386__) || defined(__amd64__))
        __asm__ __volatile__ ("pause\n");
#endif
		oldVal.f = *dst;
		newVal.f = oldVal.f + delta;
	} while (!atomicCompareAndExchange((volatile int32_t *) dst, newVal.i, oldVal.i));
	return newVal.f;
}

/**
 * \brief Atomically add \a delta to the floating point destination \a dst
 *
 * \return The final value written to \a dst
 */
inline double atomicAdd(volatile double *dst, double delta) {
	/* Atomic FP addition from PBRT */
	union bits { double f; int64_t i; };
	bits oldVal, newVal;
	do {
        // On IA64/x64, adding a PAUSE instruction in compare/exchange loops
        // is recommended to improve performance.  (And it does!)
#if (defined(__i386__) || defined(__amd64__))
        __asm__ __volatile__ ("pause\n");
#endif
		oldVal.f = *dst;
		newVal.f = oldVal.f + delta;
	} while (!atomicCompareAndExchange((volatile int64_t *) dst, newVal.i, oldVal.i));
	return newVal.f;
}

 // brief Atomically add \a delta to the 32-bit integer destination \a 
inline int32_t atomicAdd(volatile int32_t *dst, int32_t delta) {
#if defined(WIN32)
	return InterlockedExchangeAdd(reinterpret_cast<volatile LONG *>(dst), delta) + delta;
#else
	return __sync_add_and_fetch(dst, delta);
#endif
}

// brief Atomically add \a delta to the 64-bit integer destination \a dst


inline int64_t atomicAdd(volatile int64_t *dst, int64_t delta) {
#if defined(WIN64)
	return _InterlockedExchangeAdd64(reinterpret_cast<volatile LONGLONG *>(dst), delta) + delta;
#elif defined(WIN32)
	Error("atomicAdd() cannot handle 64-bit integers on WIN32");
	return 0;
#else
	return __sync_add_and_fetch(dst, delta);
#endif
}


#endif	// LM_CORE_ATOMIC_H